A StateListDrawable is a drawable object defined in XML that uses a several different images to represent the same graphic, depending on the state of the object. For example, a Button widget can exist in one of several different states (pressed, focused, or niether) and, using a state list drawable, you can provide a different background image for each state. You can describe the state list in an XML file. Each graphic is represented by an “item” element inside a single “selector” element. Each “item” uses various attributes to describe the state in which it should be used as the graphic for the drawable. During each state change, the state list is traversed top to bottom and the first item that matches the current state is used—the selection is not based on the “best match,” but simply the first item that meets the minimum criteria of the state.
翻译: StateListDrawable 是一种通过XML文件来定义的drawable,使用几个不同的图片来呈现同一个图形,通过object的状态来实现切换。例如,一个Button有几个不同的状态(按压,获取焦点等等),这种情况下,通过使用 state list drawable,你就可以实现在不同的状态下使用不同的背景图片。你可以在一个XML文件中描述state list。通过在根节点selector下定义一个item元素来添加每个图形。每一各item中使用不同的状态属性来定义不用的drawable。 当每一次状态改变的时候,state list都会从上到下被遍历一遍,第一个与当前state相匹配的item将会被使用—- 这个选择并不是作出“最匹配”结果,而是简单的找到第一个匹配的状态。